package com.sh.suhe.utildemos.views.activity.cathtml

import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.sh.suhe.utildemos.R
import com.sh.suhe.utildemos.utils.http.NetUtil
import kotlinx.android.synthetic.main.activity_article.*
import org.jsoup.Jsoup

class ArticleActivity : AppCompatActivity() {

    private var html =""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_article)
        val url =intent.getStringExtra("url")
        val title = intent.getStringExtra("title")
        article_wv.settings.defaultTextEncodingName = ("UTF-8")
        article_wv.setBackgroundColor(0) // 设置背景色
        article_wv.background.alpha = (0) // 设置填充透明度 范围：0-255
//        article_wv.loadData()
        parse(url)
    }
    val handler = Handler {
        when(it.what){
            1 ->{
                val data  = it.obj as String
                html +=data
                article_wv.loadData(html,"text/html", "UTF-8")
//                Log.e("ArticleActivity",html)
            }

        }
        true
    }
    private fun parse(url: String) {
        NetUtil.request(NetUtil.RequestMethod.GET, url, HashMap(), object : NetUtil.RequestCallBack {
            override fun onSuccess(statusCode: Int, json: String?) {
                val doc = Jsoup.parse(json)
                //获取所有的博客列表集合
                val blogList = doc.select("div[class=content]")
                //解析目录列表
                for(ele in blogList){
                    val authorEle = ele.html()

                    //通知页面刷新
                    val msg = Message()
                    msg.what = 1
                    msg.obj = authorEle
                    handler.sendMessage(msg)
                }
            }

            override fun onFailure(statusCode: Int, errorMsg: String?) {

            }

        })
    }
}
